﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MP3Ganizer.Prism;
using System.ComponentModel;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using MP3Ganizer.Common.Command;
using MP3Ganizer.Common;
using WPF.Themes;

namespace MP3Ganizer
{
    /// <summary>
    /// Interaction logic for Shell.xaml
    /// </summary>
    public partial class Shell : Window, IShellView, INotifyPropertyChanged
    {
        #region Fields
        private IUnityContainer _container;
        #endregion

        #region Commands
        #region LoadModuleOnDemandCommand
        private ICommand _LoadModuleOnDemandCommand;
        /// <summary>
        /// Commande permettant de charger un module à la demande
        /// </summary>
        public ICommand LoadModuleOnDemandCommand
        {
            get
            {
                if (this._LoadModuleOnDemandCommand == null)
                    this._LoadModuleOnDemandCommand = new RelayCommand<object>((item) => this.LoadModuleOnDemand(item), (item) => this.CanLoadModuleOnDemand());

                return this._LoadModuleOnDemandCommand;
            }
        }
        /// <summary>
        /// Fonction chargeant un module à la demande
        /// </summary>
        /// <param name="parameter"></param>
        public void LoadModuleOnDemand(object parameter)
        {
            if (parameter is String)
            {
                try
                {
                    this._container.Resolve<Microsoft.Practices.Composite.Modularity.IModuleManager>().LoadModule(parameter as String);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
            }
        }
        private bool CanLoadModuleOnDemand()
        {
            return true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //List<string> themeList = ThemeManager.GetThemes().ToList<string>();
            //themeList.Remove("ShinyDarkGreen");
            //themeList.Remove("DavesGlossyControls");
            List<string> themeList = new List<string>();
            themeList.Add("RainierRadialBlue");
            themes.ItemsSource = themeList;
            
        }

        private void themes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                string theme = e.AddedItems[0].ToString();
            }
        }
        #endregion
        #endregion

        #region Constructor
        /// <summary>
        /// Crée une instance du Shell.
        /// </summary>
        /// <param name="container">IUnityContainer de l'application</param>
        public Shell(IUnityContainer container)
        {
            InitializeComponent();
            this._container = container;
            this.DataContext = this;
        }
        #endregion

        #region Override
        /// <summary>
        /// Evènement lors de la fermeture du Shell, donc de l'application.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            this._container.Resolve<IEventAggregator>().GetEvent<MP3Ganizer.Events.Application.ApplicationEvent>().Publish(new MP3Ganizer.Events.Application.ApplicationEventArgs(Enums.ApplicationAction.Closing));
            base.OnClosing(e);
        }
        #endregion

        #region IShellView Members
        /// <summary>
        /// Implémentation de IShellView. 
        /// Lance l'affichage de la fenêtre dans Windows.
        /// </summary>
        public void ShowView()
        {
            this._container.Resolve<IEventAggregator>().GetEvent<MP3Ganizer.Events.Application.ApplicationEvent>().Publish(new MP3Ganizer.Events.Application.ApplicationEventArgs(Enums.ApplicationAction.Opening));
            this.Show();
        }

        #endregion

        #region INotifyPropertyChanged Members
        /// <summary>
        /// Implémentation de INotifyPropertyChanged.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Fonction de Raise de l'évènement PropertyChanged.
        /// </summary>
        /// <param name="PropertyName"></param>
        protected void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

        #endregion
    }
}
